<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增课程分配</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css"/>
</head>
<body>
<form class="layui-form">
    <div class="mainBox">
        <div class="main-container">
            <div class="main- container">

                <div class="layui-form-item">
                    <label class="layui-form-label">选择教师</label>
                    <div class="layui-input-block">
                        <select id="teacherId" name="teacherId"  lay-verify="required"
                                lay-reqText="请选择教师" lay-search>

                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">选择班级</label>
                    <div class="layui-input-block">
                        <select id="classes" name="classes"  lay-verify="required"
                                lay-reqText="请选择对应班级" lay-search>

                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">选择课程</label>
                    <div class="layui-input-block">
                        <select id="courseId" name="courseId"  lay-verify="required"
                                lay-reqText="请选择需要分配的课程" lay-search>

                        </select>
                    </div>
                </div>



            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
        </div>
    </div>
</form>
<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script>
    layui.use(['form', 'jquery', 'loading', 'laydate'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let loading = layui.loading;

        function getAjaxInformation(){
            //动态获取教师列表
            $.ajax({
                url: '/ClassInfo/getTeacherList',
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    //console.log(res.data);
                    let teacher = $('#teacherId');
                    teacher.append('<option value="">请选择教师</option>')
                    for (let i = 0; i < res.data.length; i++) {
                        let counselorName = res.data[i].realName;
                        let teacherId = res.data[i].teacherId;
                        teacher.append('<option value="' + teacherId + '">' + counselorName
                            + '(' + teacherId + ')' + '</option>')
                    }
                    form.render('select');
                }
            });
            //动态获取课程列表
            $.ajax({
                url: '/course/getAllCourse',
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    console.log(res.data);
                    let course = $('#courseId');
                    course.append('<option value="">请选择需要分配的课程</option>')
                    for (let i = 0; i < res.data.length; i++) {
                        let courseName = res.data[i].courseName;
                        let courseId = res.data[i].courseId;
                        course.append('<option value="' + courseId + '">' + courseName
                            + '(' + courseId + ')' + '</option>')
                    }
                    form.render('select');
                }
            });
            //动态获取班级列表
            $.ajax({
                url: '/ClassInfo/getAllClassInfo',
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    //console.log(res.data);
                    let classes = $('#classes');
                    classes.append('<option value="">请选择对应班级</option>')
                    for (let i = 0; i < res.data.length; i++) {
                        let className = res.data[i].classes;
                        classes.append('<option value="' + className + '">' + className
                            + '</option>')
                    }
                    form.render('select');
                }
            });
        }
        getAjaxInformation();
        form.on('submit(user-save)', function (data) {
            //console.log(data.field);
            loading.Load(3, "");
            $.ajax({
                url: '/relation/relationInsert',
                data: data.field,
                dataType: 'json',
                type: 'post',
                success: function (result) {
                    loading.loadRemove();
                    console.log(result);
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            //console.log(JSON.stringify(result.data));
                            parent.layui.table.reload("user-table", {
                                data: result.data,
                                page: {
                                    layout: ['count', 'prev', 'page', 'next', 'skip']
                                    , curr: parent.layui.jquery(".layui-laypage-em").next().html()
                                    , groups: 3
                                    , last: false
                                }
                            });
                        });
                    } else {
                        layer.open({
                            title: '提示'
                            , icon: 2
                            , content: result.msg
                        });
                    }
                }
            })
            return false;
        });
    })
</script>
<script>
</script>
</body>
</html>